package com.lu.model.entity;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.extension.activerecord.Model;
import lombok.*;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 商户分类
 * </p>
 *
 * @author zhanglu
 * @since 2022-09-19
 */
@Data
@EqualsAndHashCode(callSuper = false)
public class SysMechantTypes extends Model<SysMechantTypes> {

    private static final long serialVersionUID = 1L;

    @TableId(value = "id", type = IdType.AUTO)
    private Long id;

    private Long pid;

    /**
     * 级联id，pid0->pid1->pid2->id
     */
    private String cascadeIds;

    /**
     * 类型名称
     */
    private String name;

    /**
     * 卡类型id(缺损时，认为是超级卡)
     */
    private Long cardCategoryId;

    /**
     * 商品订单渠道id
     */
    private Long itemOrderChannelId;

    @Override
    protected Serializable pkVal() {
        return this.id;
    }

    /*
            -- 父 -> 子
            select * from sys_mechant_types smt1
            left join sys_mechant_types smt2 on find_in_set(smt2.id, smt1.cascade_ids)
            where smt2.name like '%A%';
            -- 子 -> 父
            select * from sys_mechant_types smt1
            left join sys_mechant_types smt2 on find_in_set(smt2.id, smt1.cascade_ids)
            where smt1.name like '%b1%';
     */

    @Data
    @Builder
    @AllArgsConstructor
    @NoArgsConstructor
    public static class TreeNodes {
        private Long id;
        private Long pid;
        private String name;
        private List<TreeNodes> childs;
    }

    public static List<TreeNodes> treeNodes(List<SysMechantTypes> objs, Long pid){
        List<TreeNodes> treeNodes = new ArrayList<>();
        if(ObjectUtil.isEmpty(objs)){
            return treeNodes;
        }
        if(pid == null){
            pid = 0L;
        }
        //pid 下的孩子
        Map<Long, List<SysMechantTypes>> objMap = objs.stream().filter(o -> o.getPid() != 0).collect(Collectors.groupingBy(SysMechantTypes::getPid));
        Long finalPid = pid;
        List<SysMechantTypes> objList = objs.stream().filter(o -> {
            if(finalPid == 0L){
                return o.getPid() == 0L;
            }else {
                return o.getId().longValue() == finalPid;
            }
        }).collect(Collectors.toList());
        for (SysMechantTypes obj : objList) {  //迭代父节点
            //父节点找孩子
            List<TreeNodes> childs = treeChildNodes(null, objMap, obj.getId());
            treeNodes.add(TreeNodes.builder().id(obj.getId()).pid(obj.getPid())
                    .name(obj.getName()).childs(childs).build());
        }
        return treeNodes;
    }

    public static List<TreeNodes> treeChildNodes(List<TreeNodes> treeNodes, Map<Long, List<SysMechantTypes>> objMap, Long pid){
        List<SysMechantTypes> objs = objMap.get(pid);
        if(ObjectUtil.isEmpty(objs)){
            return treeNodes;
        }
        if(treeNodes == null){
            treeNodes = new ArrayList<>();
        }
        for (SysMechantTypes obj : objs) {
            //父节点找孩子
            List<TreeNodes> childs = treeChildNodes(null, objMap, obj.getId());
            treeNodes.add(TreeNodes.builder().id(obj.getId()).pid(obj.getPid())
                    .name(obj.getName()).childs(childs).build());
        }
        return treeNodes;
    }

}
